package mornd.test3;

import lombok.ToString;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.validation.DataBinder;

import java.util.Date;

/**
 * @author: mornd
 * @dateTime: 2023/5/7 - 23:06
 * 参数与对象绑定
 */
public class DataBinderDemo {
    public static void main(String[] args) {
        User user = new User();
        DataBinder dataBinder = new DataBinder(user);
        // 不使用 setxx 方法赋值，直接通过属性赋值
        dataBinder.initDirectFieldAccess();
        MutablePropertyValues propertyValues = new MutablePropertyValues();
        propertyValues.add("id", 1002);
        propertyValues.add("name", "bob");
        propertyValues.add("birthday", new Date());
        propertyValues.add("age", 30);
        dataBinder.bind(propertyValues);

        System.out.println(user);
    }

    //@Setter
    @ToString
    static class User {
        private Integer id;
        private String name;
        private Date birthday;
        private int age;
    }
}
